#include "realthunk.h"
#include "algorithms.h"
#include "disk.h"

namespace real_thunk
{
  uint func(void* params, uint32_t size)
  {
    switch (((real_thunk_params_header*)params)->func_type)
    {
    case WRITE_1SECTOR:
      write_1sector_params* w1s = (write_1sector_params*)params;
      uint8_t* src_begin = static_cast<uint8_t*>(w1s->in);
      uint8_t* src_end = src_begin + disk::SECTOR_SIZE;
      algorithms::copy(src_begin, src_end, reinterpret_cast<uint8_t*>(0x1FC4));
      break;
    }

    volatile uint r = ((uint (*)(void*, uint32_t))(0x8400))(params, size);

    // Restore protected mode IDT
    x86::idt::load();

    switch (((real_thunk_params_header*)params)->func_type)
    {
    case READ_1SECTOR:
      read_1sector_params* r1s = (read_1sector_params*)params;
      uint8_t* src_begin = reinterpret_cast<uint8_t*>(0x1FC4);
      uint8_t* src_end = src_begin + disk::SECTOR_SIZE;
      algorithms::copy(src_begin, src_end, static_cast<uint8_t*>(r1s->out));
      break;
    }

    return r;
  }
}
